﻿@model VietPremier.Areas.AdminCP.Models.Promotion
@{
    ViewBag.Title = "Edit Promotion";
    Layout = "~/Areas/AdminCP/Views/Shared/AdminLayout.cshtml";
}

<script type="text/javascript" src="@Url.Content("~/Content/cssAdmin/js/plugins/jquery-1.7.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Content/cssAdmin/js/plugins/jquery-ui-1.8.16.custom.min.js")"></script>
<script type="text/javascript"  src="@Url.Content("~/ckeditor/ckeditor.js")"></script>
<script type="text/javascript" src="@Url.Content("~/ckfinder/ckfinder.js")"></script>

<script type="text/javascript">
    jQuery(document).ready(function () {
        $('#nofSuccess').hide(); // an thong bao
        $('#nofError').hide();

        readyValue();
    });
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.search);
        if (results == null)
            return "";
        else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    function readyValue() {

    }
    function CopyInformation() {
        $("#Titletag").val("Viet Premier");
        $("#Meta_description").val("Viet Premier");
        $("#Meta_keyword").val("Viet Premier");
        return false;
    }
    function alertMsg(msg) {
        $('#nofSuccess').hide();
        $('#nofError').show();
        $('#msgNofError').text(msg);
    }
    function getFileExtension(filename) {
        var ext = /^.+\.([^.]+)$/.exec(filename);
        return ext == null ? "" : ext[1];
    }
    function is_int(value) {
        for (i = 0; i < value.length; i++) {
            if ((value.charAt(i) < '0') || (value.charAt(i) > '9')) return false
        }
        return true;
    }
    function removeColorBox() {
        //tra ve mau trang thai cu
        $("#Name").css("border", "1px solid #ccc");
        $("#Image").css("border", "1px solid #ccc");
        //<!--SEO -->
        $("#Titletag").css("border", "1px solid #ccc");
        $("#Meta_description").css("border", "1px solid #ccc");
        $("#Meta_keyword").css("border", "1px solid #ccc");
        return false;
    }
    function checkField() {

        removeColorBox();

        //<!--SEO -->
        //check Title
        var Title = $("#Titletag").val();
        if (Title.length < 2 || Title.length > 200) {
            msg = "Title must have at most [2,200] characters.";
            decoded = $('<div/>').html(msg).text();
            alertMsg(decoded);
            alert(decoded);
            $("#Titletag").css("border", "1px solid red");
            $("#Titletag").focus();
            return false;
        }

        //check Meta_Description
        var MetaDescription = $("#Meta_description").val();
        if (MetaDescription.length < 2 || MetaDescription.length > 200) {
            msg = "Meta Description must have at most [2,200] characters.";
            decoded = $('<div/>').html(msg).text();
            alertMsg(decoded);
            alert(decoded);
            $("#Meta_description").css("border", "1px solid red");
            $("#Meta_description").focus();
            return false;
        }

        //check Meta_Keyword
        var MetaKeyword = $("#Meta_keyword").val();
        if (MetaKeyword.length < 2) {
            msg = "Meta Keyword must have at most 2 characters.";
            decoded = $('<div/>').html(msg).text();
            alertMsg(decoded);
            alert(decoded);
            $("#Meta_keyword").css("border", "1px solid red");
            $("#Meta_keyword").focus();
            return false;
        }

        //check image
        var image = $("#Image").val();
        if (image == null || image == "") {
            //do nothing
        }
        else {
            var ext = image.substring((Math.max(0, image.lastIndexOf(".")) || image.length) + 1);
            ext = ext.toLowerCase();

            if (ext != "jpg" && ext != "jpeg" && ext != "gif" && ext != "png") {

                msg = "! Upload an image file with format jpg, jpeg, gif or png";
                decoded = $('<div/>').html(msg).text();
                alertMsg(decoded);
                alert(decoded);
                $("#Image").css("border", "1px solid red");
                $("#Image").focus();
                return false;
            }
        }

        //check Name
        var Name = $("#Name").val();
        if (Name.length < 2 || Name.length > 100) {
            msg = "Name must have at most [2,100] characters.";
            decoded = $('<div/>').html(msg).text();
            alertMsg(decoded);
            alert(decoded);
            $("#Name").css("border", "1px solid red");
            $("#Name").focus();
            return false;
        }

        //check Description
        var Description_editor = CKEDITOR.instances.Description;
        if (Description_editor.getData() == '') {
            msg = "Description cannot be empty.";
            decoded = $('<div/>').html(msg).text();
            alertMsg(decoded);
            alert(decoded);
            CKEDITOR.instances.Description.focus();
            return false;
        }

        //check Itinerary
        var Itinerary_editor = CKEDITOR.instances.Itinerary;
        if (Itinerary_editor.getData() == '') {
            msg = "Itinerary cannot be empty.";
            decoded = $('<div/>').html(msg).text();
            alertMsg(decoded);
            alert(decoded);
            CKEDITOR.instances.Itinerary.focus();
            return false;
        }

        //check When
        var Note_editor = CKEDITOR.instances.Note;
        if (Note_editor.getData() == '') {
            msg = "Note cannot be empty.";
            decoded = $('<div/>').html(msg).text();
            alertMsg(decoded);
            alert(decoded);
            CKEDITOR.instances.Note.focus();
            return false;
        }

        //return false;
        return confirm("Are you sure you want to change?");
    }

</script>

@{
    if (!string.IsNullOrEmpty(TempData["EditSuccessfully"] as string))
    {
        var getInfo = TempData["EditSuccessfully"].ToString();
        
        <script type="text/javascript">
            jQuery(document).ready(function () {
                checkEdit();
            });
            function checkEdit() {
                msg = '@getInfo';
                $('#nofError').hide();
                $('#nofSuccess').show();
                $('#msgNofSuccess').text(msg);
                return false;
            }
        </script>
    }
}

<div class="maincontentinner">

    <ul class="maintabmenu">
        <li class="current"><a href="@Url.Action("Index", "AdminPromotion")">Quản lý khuyến mãi</a></li>
        <li class="current"><a href="@Url.Action("Insert", "AdminPromotion")">Thêm mới khuyến mãi</a></li>
    </ul>
    <!--maintabmenu-->

    <div class="notification msgsuccess" style="display: none" id="nofSuccess">
        <a class="close"></a>
        <p id="msgNofSuccess">This is a success message.</p>
    </div>
    <!-- notification msgsuccess -->

    <div class="notification msgerror" style="display: none" id="nofError">
        <a class="close"></a>
        <p id="msgNofError">This is an error message.</p>
    </div>
    <!-- notification msgerror -->

    <form id="formCreate" class="stdform stdform2" method="post" action="#" enctype="multipart/form-data">

        <div class="contenttitle radiusbottom0">
            <h2 class="table"><span>Quản lý SEO</span></h2>
        </div>

        <p>
            <label>Title (*)</label>
            <span class="field">
                <input type="text" name="Titletag" id="Titletag" class="longinput" value="@Model.Titletag"/></span>
        </p>

        <p>
            <label>Meta Description (*)</label>
            <span class="field">
                <input type="text" name="Meta_description" id="Meta_description" class="longinput" value="@Model.Meta_description"/></span>
        </p>

        <p>
            <label>Meta Keyword (*)</label>
            <span class="field">
                <textarea class="longinput" rows="5" cols="80" name="Meta_keyword" id="Meta_keyword">@Model.Meta_keyword</textarea></span>
        </p>

        <p class="stdformbutton" style="padding:10px">
            <input type="submit" value="Use The Default SEO" onclick="return CopyInformation()" />
        </p>

        <div class="contenttitle radiusbottom0">
            <h2 class="table"><span>Sửa thông tin khuyến mãi</span></h2>
        </div>
        <!--contenttitle-->

        <p>
            <label>Ảnh hiện tại</label>
            <span class="field">
               @{ 
                   var imageFile = "~/Content/upload/promotions/";
                   if (Model.Image != null) {
                       imageFile += Model.Image;
                    }
                    else {
                        imageFile += "no-photo.jpg";
                    }
                }
                <img id="CurrentImage" src="@Url.Content(imageFile)" alt="" style="height: 100px; width: 100px" />
            </span>
        </p>

        <p>
            <label>Thay đổi ảnh</label>
            <span class="field">
                <input type="file" name="Image" id="Image" />
            </span>
        </p>

        <p>
            <label>Tên</label>
            <span class="field">
                <input type="text" name="Name" id="Name" class="longinput" value="@Model.Name" />
            </span>
        </p>

        <p>
            <label>Mô tả</label>
            <span class="field">
                <textarea class="ckeditor" rows="10" cols="80" name="Description" id="Description">@Model.Description</textarea>
            </span>
            <script type="text/javascript">
                CKEDITOR.replace('Description', {
                    filebrowserBrowseUrl: '/ckfinder/ckfinder.html',
                    filebrowserImageBrowseUrl: '/ckfinder/ckfinder.html?Type=Images',
                    filebrowserFlashBrowseUrl: '/ckfinder/ckfinder.html?Type=Flash',
                    filebrowserUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                    filebrowserImageUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                    filebrowserFlashUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
                });
            </script>
        </p>

        <p>
            <label>Hành trình</label>
            <span class="field">
                <textarea class="ckeditor" rows="10" cols="80" name="Itinerary" id="Itinerary">@Model.Itinerary</textarea>
            </span>
            <script type="text/javascript">
                CKEDITOR.replace('Itinerary', {
                    filebrowserBrowseUrl: '/ckfinder/ckfinder.html',
                    filebrowserImageBrowseUrl: '/ckfinder/ckfinder.html?Type=Images',
                    filebrowserFlashBrowseUrl: '/ckfinder/ckfinder.html?Type=Flash',
                    filebrowserUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                    filebrowserImageUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                    filebrowserFlashUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
                });
            </script>
        </p>

        <p>
            <label>Lưu ý</label>
            <span class="field">
                <textarea class="ckeditor" rows="10" cols="80" name="Note" id="Note">@Model.Note</textarea>
            </span>
            <script type="text/javascript">
                CKEDITOR.replace('Note', {
                    filebrowserBrowseUrl: '/ckfinder/ckfinder.html',
                    filebrowserImageBrowseUrl: '/ckfinder/ckfinder.html?Type=Images',
                    filebrowserFlashBrowseUrl: '/ckfinder/ckfinder.html?Type=Flash',
                    filebrowserUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                    filebrowserImageUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                    filebrowserFlashUploadUrl: '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
                });
            </script>
        </p>

        <p class="stdformbutton">
            <input type="submit" value="Accept" onclick="return checkField()" />
            <input type="submit" value="Back" onclick="javascript: history.back(); return false" />
        </p>

    </form>
    <br clear="all" />

</div>
<!--maincontentinner-->
